<template>
  <div class="container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="form">
      <el-form-item label="预警阈值" prop="threshold">
        <el-input v-model="form.threshold" style="width: 200px" placeholder="请输入预警阈值"></el-input>
      </el-form-item>
      <el-form-item label="是否开启" prop="isOpen">
        <el-switch v-model="form.isOpen"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit">保存</el-button>
        <el-button size="small" type="info" @click="inventoryAlertInfo">查看预警记录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import {getInventoryAlertBean, setInventoryAlertBean, userRegister} from '@/api/user'

export default {
  created() {
    this.getInventoryAlertInfo()
  },
  data () {
    return {
      form: {
        isOpen: false,
        threshold: ''
      },
      rules: {
        isOpen: [
          { required: true, trigger: 'change' }
        ],
        threshold: [
          { required: true, message: '请输入阈值', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    getInventoryAlertInfo() {
      getInventoryAlertBean().then(res=> {
        // console.log(res.data)
        this.form = res.data
      })
    },
    onSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setInventoryAlertBean(this.form).then(res=>{
            if (res.data) {
              this.$message({
                message: '预警设置成功',
                type: 'success'
              })
            } else {
              this.$message({
                message: '预警设置失败',
                type: 'error'
              })
            }
          })
        }
      })
    },
    inventoryAlertInfo () {
      this.$router.push('InventoryAlertInfo')
    }
  }
}
</script>

<style lang="scss">
.container {
  padding: 50px;
  display: flex;
  justify-content: center;
  .form {
    width: 300px;
  }
}
</style>
